Use case: External event capture with DMA

Sequence diagram

@startuml
' To add a number by line
' autonumber
' Fix order of each column

participant "User Application" as App
participant "HAL HRTIM" as Driver
participant "HAL DMA" as DMA

group "HRTIM Initialization"
  App -[hidden]-> Driver
end

group "DMA channel Initialization (Peripheral to Memory)"
  App -> DMA : DMA channel initialization
  App -> Driver : __HAL_LINKDMA(&hhrtim, hdmaTimerA, hdma)
  Driver -> Driver : DMA channel handle associated to Timer A\nis stored into the HRTIM handle
end

group "Timer A configuration"
  note right of App : dma_config must be persistent\n(accessed through the HRTIM handle by\nHAL_HRTIM_WaveformCountStart_DMA function)

  App -> Driver : HAL_HRTIM_WaveformTimerConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_A, &sConfig_timerA)
  Driver -> Driver : Pointer to Timer A DMA configuration\nstored into HRTIM handle
  Driver --> App : HAL_OK

  App --> Driver : HAL_HRTIM_EventConfig(&hhrtim, HRTIM_EVENT_3, &event_config)
  Driver --> App : HAL_OK

  App -> Driver : HAL_HRTIM_TimerEventFilteringConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_A, HRTIM_EVENT_3, &timer_event_config)
  Driver --> App : HAL_OK

  App -> Driver : HAL_HRTIM_TimeBaseConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_A, &sConfig_time_base)
  Driver --> App : HAL_OK

  App -> Driver : HAL_HRTIM_WaveformCaptureConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_A, HRTIM_CAPTUREUNIT_1, &capture_unit_1_config)
  Driver --> App : HAL_OK
end

group "Timer B configuration"
  App -> Driver : HAL_HRTIM_TimeBaseConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_B, &sConfig_time_base)
  Driver --> App : HAL_OK

  App -> Driver : HAL_HRTIM_WaveformCompareConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_B, HRTIM_COMPAREUNIT_1, &sConfig_compare)
  Driver --> App : HAL_OK
end

group "Start input capture (DMA mode)"
  App -> Driver : HAL_HRTIM_WaveformCountStart_DMA(&hhrtim, (HRTIM_TIMERID_TIMER_A | HRTIM_TIMERID_TIMER_B))
  Driver -> Driver : Enable capture 1 DMA request
  Driver -> DMA : Start DMA transfer in interrupt mode
  Driver --> App : HAL_OK
end

@enduml

Code snippet

HAL1

HAL2

Usage

 HRTIM_HandleTypeDef                 hhrtim;

 HRTIM_TimeBaseCfgTypeDef            sConfig_time_base;
 HRTIM_TimerCfgTypeDef               sConfig_timerA;
 HRTIM_EventCfgTypeDef               event_config;
 HRTIM_TimerEventFilteringCfgTypeDef timer_event_config;
 HRTIM_CompareCfgTypeDef             sConfig_compare;

 hhrtim1.Instance = HRTIM1;
 hhrtim1.Init.HRTIMInterruptResquests = HRTIM_IT_NONE;
 hhrtim1.Init.SyncOptions = HRTIM_SYNCOPTION_NONE;
 HAL_HRTIM_Init(&hhrtim);

/* Enable peripherals: HRTIM and GPIO Clocks, configure GPIO Pin, and link DMA channel handle
to HRTIM TIMER A handle*/
 .................
 ................

 /* Timer A configuration */
 sConfig_timerA.InterruptRequests = HRTIM_TIM_IT_NONE;
 sConfig_timerA.DMARequests = HRTIM_TIM_DMA_CPT1;
 sConfig_timerA.DMASrcAddress = (uint32_t)DMATransferSrc;
 sConfig_timerA.DMADstAddress = (uint32_t)DMATransferDest;
 sConfig_timerA.DMASize = 12;
 sConfig_timerA.HalfModeEnable = HRTIM_HALFMODE_DISABLED;
 sConfig_timerA.InterleavedMode = HRTIM_INTERLEAVED_MODE_DISABLED;
 sConfig_timerA.StartOnSync = HRTIM_SYNCSTART_DISABLED;
 sConfig_timerA.ResetOnSync = HRTIM_SYNCRESET_DISABLED;
 sConfig_timerA.DACSynchro = HRTIM_DACSYNC_NONE;
 sConfig_timerA.PreloadEnable = HRTIM_PRELOAD_DISABLED;
 sConfig_timerA.UpdateGating = HRTIM_UPDATEGATING_INDEPENDENT;
 sConfig_timerA.BurstMode = HRTIM_TIMERBURSTMODE_MAINTAINCLOCK;
 sConfig_timerA.DeadTimeInsertion = HRTIM_TIMDEADTIMEINSERTION_DISABLED;
 sConfig_timerA.RepetitionUpdate = HRTIM_UPDATEONREPETITION_DISABLED;
 sConfig_timerA.ReSyncUpdate = HRTIM_TIMERESYNC_UPDATE_UNCONDITIONAL;
 HAL_HRTIM_WaveformTimerConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_A, &sConfig_timerA);

 event_config.Source = HRTIM_EEV6SRC_GPIO;
 event_config.Sensitivity = HRTIM_EVENTSENSITIVITY_RISINGEDGE;
 HAL_HRTIM_EventConfig(&hhrtim, HRTIM_EVENT_3, &event_config)

 timer_event_config.Filter = HRTIM_TIMEVENTFILTER_BLANKINGCMP1;
 timer_event_config.Latch = HRTIM_TIMEVENTLATCH_DISABLED;
 HAL_HRTIM_TimerEventFilteringConfig(&hhrtim,  HRTIM_TIMERINDEX_TIMER_A, HRTIM_EVENT_3, &timer_event_config);

 sConfig_time_base.Period = 0x2000;
 sConfig_time_base.RepetitionCounter = 0x00;
 sConfig_time_base.PrescalerRatio = HRTIM_PRESCALERRATIO_DIV4;
 sConfig_time_base.Mode = HRTIM_MODE_CONTINUOUS;
 HAL_HRTIM_TimeBaseConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_A, &sConfig_time_base);

 capture_unit_1_config.Trigger = HRTIM_CAPTURETRIGGER_EEV_3;
 HAL_HRTIM_WaveformCaptureConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_A, HRTIM_CAPTUREUNIT_1, &capture_unit_1_config);

/* Timer B configuration */
 sConfig_time_base.Period = 0x2000;
 sConfig_time_base.RepetitionCounter = 0x00;
 sConfig_time_base.PrescalerRatio = HRTIM_PRESCALERRATIO_DIV4;
 sConfig_time_base.Mode = HRTIM_MODE_CONTINUOUS;
 HAL_HRTIM_TimeBaseConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_B, &sConfig_time_base);

 /* Set compare register 1 in Timer B */
 sConfig_compare.CompareValue = 0x100;
 HAL_HRTIM_WaveformCompareConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_B, HRTIM_COMPAREUNIT_1, &sConfig_compare);

 /* Start HRTIM TIMER A in DMA mode and HRTIM TIMER B in polling mode*/
 HAL_HRTIM_WaveformCountStart_DMA(&hhrtim, HRTIM_TIMERINDEX_TIMER_A | HRTIM_TIMERINDEX_TIMER_B);
  hal_hrtim_handle_t                 hhrtim;

  hal_hrtim_timer_config_t           timer_config;
  hal_hrtim_dma_config_t             dma_config;
  hal_hrtim_eev_config_t             evt3_config;
  hal_hrtim_eev_filter_config_t      evt3_filter;

 HAL_HRTIM_Init(&hhrtim, HAL_HRTIM1);

 /* Enable peripherals: HRTIM and GPIO Clocks, configure GPIO Pin, and link DMA channel handle
 to HRTIM TIMER A handle*/
  .................
  ................

 /* Timer A configuration */
 dma_config.requests = HAL_HRTIM_OPT_DMA_CPT1;
 dma_config.dest_addr = (uint32_t)DMATransferDest;
 dma_config.src_addr = (uint32_t)DMATransferSrc;
 dma_config.size_byte = 12;
 HAL_HRTIM_SetConfigDMA(&hhrtim, HAL_HRTIM_DMA_TIMA,&dma_config);

 /* Configure external events sensitivity */
 evt3_config.source = HAL_HRTIM_EEV3_GPIO;
 evt3_config.sensitivity = HAL_HRTIM_EEV_EDGE_RISING;
 HAL_HRTIM_EEV_SetConfig(&hhrtim, HAL_HRTIM_EEV3, &evt3_config)

 evt3_filter.filter = HAL_HRTIM_BLK_RST_CMP1;
 evt3_filter.notification = HAL_HRTIM_EEV_IMMEDIATE;
 HAL_HRTIM_TIM_SetEventFilter(&hhrtim, HAL_HRTIM_TIMA, HAL_HRTIM_EEV3, &evt3_filter);

 timer_a_config.period = 0x2000;
 timer_a_config.repetition = 0x00;
 timer_a_config.prescaler = HAL_HRTIM_TIM_PRESCALER_DIV4;
 timer_a_config.operating_mode = HAL_HRTIM_TIM_MODE_CONTINUOUS;
 HAL_HRTIM_TIM_SetConfig(&hhrtim, HAL_HRTIM_TIMA,  &timer_config);

 HAL_HRTIM_TIM_SetCaptureTrigger(&hhrtim, HAL_HRTIM_TIMA, HAL_HRTIM_CAPTURE_UNIT_1, HAL_HRTIM_EEV3);

/* Timer B configuration */
 timer_b_config.period = 0x2000;
 timer_b_config.repetition = 0x00;
 timer_b_config.prescaler = HAL_HRTIM_TIM_PRESCALER_DIV4;
 timer_b_config.operating_mode = HAL_HRTIM_TIM_MODE_CONTINUOUS;
 HAL_HRTIM_TIM_SetConfig(&hhrtim, HAL_HRTIM_TIMB, &timer_config);

 /* Set compare register 1 in Timer B */
 HAL_HRTIM_TIM_SetCompareValue(&hhrtim, HAL_HRTIM_TIMB, HAL_HRTIM_COMPARE_UNIT_1, 0x100);

 /* Start HRTIM TIMER A in DMA mode and HRTIM TIMER B in polling mode*/
 HAL_HRTIM_TIM_Start_DMA(&hhrtim,  HAL_HRTIM_TIMA | HAL_HRTIM_TIMB);

Key changes

  • HAL2 links the DMA channel via HAL_HRTIM_SetDMA() and configures DMA requests via HAL_HRTIM_SetConfigDMA().

  • HAL2 uses dedicated event and filtering APIs (for example HAL_HRTIM_EVT_SetConfig() and HAL_HRTIM_TIM_SetEventFilter()).

  • HAL2 starts capture in DMA mode with HAL_HRTIM_TIM_Start_DMA() using explicit timer selection.